package com.zzb.valid.util;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.lang.Nullable;

import java.util.Locale;

/**
 * @author create by zhouzongbo on 2020/10/27.
 */
public class I18nUtil {
    private static MessageSource messageSource;

    private I18nUtil() {

    }

    public static void setMessageSource(MessageSource messageSource) {
        I18nUtil.messageSource = messageSource;
    }
    public static String getMessage(String code, Object... args) {
        if (code == null) {
            return null;
        }
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(code, args, null, locale);
    }

    public static String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage) {
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(code, args, defaultMessage, locale);
    }
}
